#pragma once
#pragma warning(disable:4996)       // disable checked iterator errors http://msdn.microsoft.com/en-us/library/aa985965(VS.80).aspx 

//
// Copyright (C) 2011 - 2013 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1

#include <Macros.h>
#include <ValueHelpers.h>
#include <Settings.h>

#pragma unmanaged 
#include <ql\experimental\credit\distribution.hpp>
#include <boost/smart_ptr/detail/spinlock.hpp>
#pragma managed 

using namespace System;
using namespace QuantLib;
using namespace Cephei;
using namespace Cephei::Core;
using namespace PLATFORM::Collections;

#undef HANDLE
#undef ABSTRACT
#define STRUCT
namespace Cephei { namespace QL { namespace Experimental { namespace Credit {
	//////////////////////////////////////////////////////////////////////////////////////////////
	// implementation of IDistribution
	public ref class CDistribution  : 
            public Cephei::QL::Experimental::Credit::IDistribution
	{
	protected: 
		boost::shared_ptr<QuantLib::Distribution>* _ppDistribution;
        boost::detail::spinlock* _pSpinlock;
#ifdef HANDLE
		QuantLib::Handle<QuantLib::Distribution>* _phDistribution;
#endif
		Object^ _DistributionOwner;     // reference to object that manages the storage for this object
	internal:
		CDistribution ();
		CDistribution (Int32 nBuckets, Double xmin, Double xmax);
        CDistribution (boost::shared_ptr<QuantLib::Distribution>& childNative, Object^ owner);
        CDistribution (QuantLib::Distribution& childNative, Object^ owner);
        CDistribution (CDistribution^ copy);
        CDistribution (PLATFORM::Type^ t);
#ifdef STRUCT
        CDistribution (QuantLib::Distribution childNative);
#endif       
#ifdef HANDLE
		CDistribution (QuantLib::Handle<QuantLib::Distribution>& childNative, Object^ owner);
		CDistribution (QuantLib::Handle<QuantLib::Distribution> childNative);
#endif
		virtual ~CDistribution ();
		!CDistribution ();

	internal:
		QuantLib::Distribution& GetReference ();
		boost::shared_ptr<QuantLib::Distribution>& GetShared ();
		QuantLib::Distribution* GetPointer ();
        void SetDistribution (boost::shared_ptr<QuantLib::Distribution> native)
        {
            if (_ppDistribution != NULL)
                delete _ppDistribution;
            _ppDistribution = new boost::shared_ptr<QuantLib::Distribution> (native);
            
        }
#ifdef HANDLE
		QuantLib::Handle<QuantLib::Distribution>& GetHandle ();
#endif
		virtual bool HasNative () ;
		void Lock ()
		{
			if (Cephei::QL::Settings::_ReferenceLocking)
    			_pSpinlock->lock ();
		}
		void Unlock ()
		{
			_pSpinlock->unlock ();
		}
    public:
		virtual Cephei::QL::Experimental::Credit::IDistribution^ Add (Double value) ;
		virtual Cephei::QL::Experimental::Credit::IDistribution^ AddAverage (Int32 bucket, Double value) ;
		virtual Cephei::QL::Experimental::Credit::IDistribution^ AddDensity (Int32 bucket, Double value) ;
		virtual Double Average (UInt64 k) ;
		virtual Double ConfidenceLevel (Double quantil) ;
		virtual Double Cumulative (UInt64 k) ;
		virtual Double CumulativeDensity (Double x) ;
		virtual Double CumulativeExcess (UInt64 k) ;
		virtual Double CumulativeExcessProbability (Double a, Double b) ;
		virtual Double Density (UInt64 k) ;
		virtual Double Dx (Double x) ;
		virtual Cephei::Core::IVector<Double>^ Dx () ;
		virtual Double Dx (UInt64 k) ;
		virtual Double Excess (UInt64 k) ;
		virtual Double ExpectedValue () ;
		virtual Int32 Locate (Double x) ;
        property Cephei::QL::Experimental::Credit::IDistribution^ Normalize 
        {
		    virtual Cephei::QL::Experimental::Credit::IDistribution^ get () ;
        }
        property UInt64 Size 
        {
		    virtual UInt64 get () ;
        }
		virtual Cephei::QL::Experimental::Credit::IDistribution^ Tranche (Double attachmentPoint, Double detachmentPoint) ;
		virtual Double TrancheExpectedValue (Double a, Double d) ;
		virtual Cephei::Core::IVector<Double>^ X () ;
		virtual Double X (UInt64 k) ;
    };
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Factory class
//z	[FactoryFor(Core::Generic::ICoCell<Cephei::QL::Experimental::Credit::IDistribution^>::typeid)]
	[FactoryFor(Cephei::QL::Experimental::Credit::IDistribution::typeid)]
	[FactoryFor(Cephei::QL::Experimental::Credit::IDistribution_Factory::typeid)]
	public ref class CDistribution_Factory sealed : public IDistribution_Factory
	{
	public:
        virtual IDistribution^ Create ();
        virtual IDistribution^ Create (Int32 nBuckets, Double xmin, Double xmax);
    };
   
/*Cephei*/ } /*QL*/ } /*Experimental*/ } /*Credit */}
